package com.cc8w.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by sang on 2017/12/25.
 */
@MappedJdbcTypes(JdbcType.DATE)
@MappedTypes(String.class)
public class DateTypeHandler implements TypeHandler<String> {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    @Override
    public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
    	System.out.println("时间类型转换器");
    }

    @Override
    public String getResult(ResultSet rs, String columnName) throws SQLException {
    	System.out.println("时间类型转换器1");
        return sdf.format(new Date(rs.getDate(columnName).getTime()) );
    }

    @Override
    public String getResult(ResultSet rs, int columnIndex) throws SQLException {
    	System.out.println("时间类型转换器2");
        return sdf.format(new Date(rs.getDate(columnIndex).getTime()));
    }

    @Override
    public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
    	System.out.println("时间类型转换器3");
    	return sdf.format(new Date(cs.getDate(columnIndex).getTime()));
    }
}
